﻿--[[
   @brief    run-game popup window
   @author   rudolfzhang
   @version  1.0
   @date     2015-03-27
--]]


popup_wnd_view = {

    init = function()

    end;

    show_popup_wnd = function(is_show)
        if is_show == 0 then
            local uilib = ui_utility.uilib();
            poptips_view.close(uilib);
        end;
    end;

    repair_btn_clicked = function(uilib, dwID_Click)
        ierd_tgp.tpf_ui.Log("repair_btn_clicked");
        -- 修复游戏按钮
        event_center.send_event(tool_box_event.OPEN_CF_REPAIR_PAGE, "");
        popup_wnd_view.show_popup_wnd(FALSE);
    end;

    exit_game_btn_clicked = function(uilib, dwID_Click)
        ierd_tgp.tpf_ui.Log("exit_game_btn_clicked");
        -- 退出游戏按钮
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        game_lib:stop_game(popup_wnd_model.action_info.game_id_);
        popup_wnd_view.show_popup_wnd(FALSE);
    end;

    identity_verification_btn_clicked = function(uilib, dwID_Click)
        ierd_tgp.tpf_ui.Log("identity_verification_btn_clicked");
        -- 实名认证按钮
        local url_str = LoadStr("JKYX_REGEDIT_URL");
        web_utility.open_browser(url_str, true, true);
        popup_wnd_view.show_popup_wnd(FALSE);
    end;

    cancel_btn_clicked = function(uilib, dwID_Click)
        ierd_tgp.tpf_ui.Log("cancel_btn_clicked");
        -- 取消按钮
        popup_wnd_view.show_popup_wnd(FALSE);
    end;

    kill_update = function()
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        -- 把当前游戏进程干掉
        game_lib:kill_update(popup_wnd_model.kill_process_info);
        popup_wnd_view.show_popup_wnd(FALSE);
        -- 继续更新游戏  
        local action = ierd_tgp.game_library.GameAction:new();
        action.action_type = ierd_tgp.game_library.GameAction.kUpdateGame;
        action.game_id = popup_wnd_model.kill_process_info.game_id;
        action.server_id = popup_wnd_model.kill_process_info.server_id;
        action.op_from = popup_wnd_model.op_from;

        -- 延迟1秒执行更新
        game_lib:DelayAction(action, 1500);

        action:delete();

        event_center.send_event(main_event.UPDATE_GAME_START, "");
    end;

    preupdate_game_btn_clicked = function(uilib, dwID_Click)
        ierd_tgp.tpf_ui.Log("preupdate_game_btn_clicked");
        local data_out = comm_center.call_service("i_service_allow_continue_operation_game",
                            {game_id = popup_wnd_model.action_info.game_id_});
        if data_out ~= nil and not data_out.bAllowContinue then
            return
        end
        -- 预更新按钮
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        local op_from = ierd_tgp.game_library.LAUNCH_OP_FROM_MAIN_WND;
        if nil ~= popup_wnd_model.action_info.launch_op_from and 0 ~= popup_wnd_model.action_info.launch_op_from then
            op_from = popup_wnd_model.action_info.launch_op_from;
        end
        game_lib:update_game(popup_wnd_model.action_info.game_id_, popup_wnd_model.action_info.zone_id_, op_from);
        event_center.send_event(main_event.UPDATE_GAME_START, "");
        popup_wnd_view.show_popup_wnd(FALSE);
    end;
    
    tas_scandangers_modpwd_clicked = function(uilib, dwID_Click)
        ierd_tgp.tpf_ui.Log("tas_scandangers_modpwd_clicked");
        local param_str = string.format('{"type":"open_web", "browser":"sys", "url":"%s"}', LoadStr("CHANGE_PSW_URL"));
        web_utility.open_link(param_str);
        --event_center.send_event(login_event.LOGIN_EXIT_TGP, "");
        comm_center.call_service("Svr_ExitWeGame", {});
    end;

    tas_scandangers_exit_clicked = function(uilib, dwID_Click)
        ierd_tgp.tpf_ui.Log("tas_scandangers_exit_clicked");
        --event_center.send_event(login_event.LOGIN_EXIT_TGP, "");
        comm_center.call_service("Svr_ExitWeGame", {});
    end;

    OnOMDInstallWarningClose = function()
        ierd_tgp.tpf_ui.Log("OnOMDInstallWarningClose");      
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        game_lib:OnOMDInstallWarningClosed();
    end;
    
    OnOMDInstallWarningOkClicked = function(uilib, dwID_Click)
        ierd_tgp.tpf_ui.Log("OnOMDInstallWarningOkClicked");      
        popup_wnd_view.show_popup_wnd(FALSE);
        popup_wnd_view.OnOMDInstallWarningClose();
    end;
    
    OnOMDInstallWarningCancelClicked = function(uilib, dwID_Click)
        ierd_tgp.tpf_ui.Log("OnOMDInstallWarningCancelClicked");
        popup_wnd_view.OnOMDInstallWarningClose();
    end;
    
    btn_enter_low_speed_clicked = function (uilib, dwID_Click, user_data, addon_info)
        base_utility.log("[popup wnd]btn_enter_low_speed_clicked, user_data:" .. tostring(user_data));
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        if not game_lib then
            base_utility.log("[popup_wnd][btn_enter_low_speed_clicked]game_lib is nil");
            return;
        end    

        local checked = 0;
        if user_data and addon_info and addon_info.cbx_dont_ask_checked then
            checked = 1;
            game_lib:DontShowLowspeedMode(user_data);
        end

        game_lib:ChangeDownloadMode(ierd_tgp.game_library.DownloadMode.kLowSpeedMode);     

        popup_wnd_view.report_click_url(
            ierd_tgp.adapt_for_imports.qos.Qos_url_click_type.kUrlTypeLowspeedEnter,
            user_data, checked);
    end;
    
    btn_pause_preudpate_clicked = function (uilib, dwID_Click, user_data, addon_info)
        base_utility.log("[popup wnd]btn_pause_preudpate_clicked, user_data" .. tostring(user_data));
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        if not game_lib then
            base_utility.log("[popup_wnd][btn_pause_preudpate_clicked]game_lib is nil");
            return;
        end
            
        if not user_data then 
            base_utility.log("[popup_wnd][btn_pause_preudpate_clicked]user_data is nil");
            return;
        end
            
        local checked = 0;
        if user_data and addon_info and addon_info.cbx_dont_ask_checked then     
            checked = 1;
            game_lib:DontShowLowspeedMode(user_data);
        end

        game_lib:pause_preupdate_game_directlly(user_data, ierd_tgp.game_library.OperationSource.kPauseByUser);  
        state_operation_panel.set_preupdate_mutex(game_id, false);   

        popup_wnd_view.report_click_url(
            ierd_tgp.adapt_for_imports.qos.Qos_url_click_type.kUrlTypeLowspeedPause,
            user_data, checked);
    end;

    btn_matrix_reload_clicked = function (uilib, ctr_id)
        -- 关闭弹窗
        local content_id = ui_utility.get_ctrl_parent_id(ctr_id);
        if content_id <= 0 then
            return;
        end
        local win_id = ui_utility.get_ctrl_parent_id(content_id);
        if win_id <= 0 then
            return;
        end
        poptips_view:_close(uilib, win_id);
        
        local login_mgr = base_utility.get_component_adapter("ILogin_mgr",
                    "ierd_tgp.login_mgr.GetLoginMgr();");
        if login_mgr ~= nil then
            login_mgr:ClickLoadMatrixFail(true);
        end
    end;

    matrix_reload_error_layout = function (win_id, opts)
        -- body
        base_utility.log("[popup_wnd] matrix reload error is under layout.");
    end;

    btn_exit_tgp_clicked = function (uilib, dwID_Click, user_data, addon_info)
        base_utility.log("[popup wnd]btn_exit_tgp_clicked" );
        event_center.send_event(group_event.TGP_CLOSE, "{}");
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        if not game_lib then
            base_utility.log("[popup_wnd][btn_exit_tgp_clicked]game_lib is nil");
            return;
        end
             
        local checked = 0;
        if user_data and addon_info and addon_info.cbx_dont_ask_checked then      
            checked = 1;
            game_lib:DontShowLowspeedMode(user_data);
        end
        layout_mgr.hide_all_window();
        game_lib:do_exit_app();   

        popup_wnd_view.report_click_url(
            ierd_tgp.adapt_for_imports.qos.Qos_url_click_type.kUrlTypeLowspeedExit,
            user_data, checked);  
    end;
    
    restart_tgp = function ()
        base_utility.log("[popup wnd]restart_tgp" );
        local gamelib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        if gamelib ~= nil then
            gamelib:do_update_app();
        end;
        -- 弹框消失
        popup_wnd_view.show_popup_wnd(FALSE);
    end;

    report_click_url = function(action, game_id, msg_id)
        local qosparam = ierd_tgp.tpf_ui.LuaQosData:new();
        qosparam.oc_type = ierd_tgp.tpf_ui.kInstant;
        qosparam.qos_report_id = ierd_tgp.adapt_for_imports.qos.Qos_kind.kUrlClick; --url上报
        qosparam.int_count = 3;
        qosparam.str_count = 1;
        qosparam.int_val[0]= action;
        qosparam.int_val[1]= game_id;
        qosparam.int_val[2]= msg_id;
        qosparam.str_val1 = tostring(game_id);
        base_utility.lua_qos(qosparam);
        qosparam:delete();
    end;

    wegame_qualify_login_tips_close = function(is_jump, link_url)
        base_utility.log("[wegame_qualify_login_tips_close] is jump: " .. tostring(is_jump));
        if is_jump == true and link_url ~= nil and link_url ~= "" then
            base_utility.log("[wegame_qualify_login_tips_close] jump: " .. tostring(link_url));
            web_utility.open_link('{"type":"open_web", "browser":"sys", "url":"'..tostring(link_url).. '", "use_ptlogin":"1"}');
        end;
        popup_wnd_view.show_popup_wnd(FALSE);
    end;
}

popup_wnd_view.init();